package com.dense.kuiniu.bookborrow.business.home.query;

import android.view.View;
import android.widget.EditText;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.base.BaseFragment;
import com.dense.kuiniu.bookborrow.base.SpKey;
import com.dense.kuiniu.bookborrow.base.Url;
import com.dense.kuiniu.bookborrow.business.adapter.BookTagAdapter;
import com.dense.kuiniu.bookborrow.business.entity.Book;
import com.dense.kuiniu.bookborrow.debug.DataProvider;
import com.dense.kuiniu.bookborrow.utils.InputUtils;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.rxjava.rxlife.RxLife;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import rxhttp.RxHttp;

/**
 * @author Administrator
 * @date 2020/8/7
 */
public class QueryDelegate extends BaseFragment {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.et_search)
    EditText mEtSearch;
    private BookTagAdapter mBookTagAdapter;

    public static QueryDelegate getInstance() {
        return new QueryDelegate();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.delegate_query;
    }

    @Override
    protected void init() {
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getContext());
        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setAlignItems(AlignItems.STRETCH);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mBookTagAdapter = new BookTagAdapter(R.layout.item_book_tag, DataProvider.getBookList()));
        mBookTagAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Book book = (Book) adapter.getItem(position);
                if (book != null) {
                    start(BookDetailDelegate.getInstance(book));
                }
            }
        });
        queryHotBook();
    }

    private void queryHotBook() {
        RxHttp.get(Url.queryBookList)
                .add("libraryCode", SPUtils.getInstance().getString(SpKey.KEY_BINDING_LIBRARY, "00"))
                .add("searchKey", "")
                .add("searchType", 0)
                .add("pageSize", 10)
                .add("currentPage", 1)
                .asResponsePageList(Book.class)
                .to(RxLife.toMain(this))  //感知生命周期，并在主线程回调
                .subscribe(pageList -> {
                    if (mBookTagAdapter != null) {
                        mBookTagAdapter.setNewInstance(pageList.getList());
                    }
                }, throwable -> {
                    ToastUtils.showShort(throwable.getMessage());
                });
    }

    @OnClick({R.id.iv_search, R.id.tv_exit})
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    protected void onClick2(View view) {
        int id = view.getId();
        if (id == R.id.iv_search) {
            InputUtils.hideKeyboard(view, getActivity());
            search(mEtSearch.getText().toString());
        } else if (id == R.id.tv_exit) {
            pop();
        }
    }

    private void search(String key) {
        QueryListDelegate delegate = findFragment(QueryListDelegate.class);
        if (delegate == null) {
            delegate = QueryListDelegate.getInstance(key);
        }
        startWithPop(delegate);
    }
}